'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 清除CD
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var rewardService = require('../../../../services/rewardService');
var buildService = require('../../../../services/buildService');
var dataApi = require('../../../../libs/dataApi.js');
var actCarbonConf = require('../../../../config/actCarbonConf');
var taskService = require('../../../../services/taskService.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var carbon_id = msg.record_id;
    var battle_result = msg.result;

    if (!carbon_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var model_name;
    var carbon_conf = actCarbonConf.getCarbonConfById(carbon_id);
    if (carbon_conf.type === 1) {
        model_name = 'gold_carbon';
    } else {
        model_name = 'food_carbon';
    }

    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    [model_name, 'role', 'build', 'soldier', 'bag', 'tech', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var act_carbon_model = player_manager[model_name];
                var role_model = player_manager.role;

                // // 开放等级判断
                // var active_carbon_open_lvl = constantUtil.value('active_carbon_open_lvl");
                // if (role_model.lv < active_carbon_open_lvl) {
                //     return next(null, {
                //         code: Code.ACTIVE_CARBON_NOT_OPEN
                //     });
                // }

                if (battle_result === 1) {
                    // 发放奖励
                    reward_list = actCarbonConf.getCarbonReward(carbon_id);
                    rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_CARBON, operationType.OPR_ACT_CARBON_FIGHT);

                    if (act_carbon_model.carbon_passed < carbon_id) {
                        // 设置通关副本
                        act_carbon_model.carbon_passed = carbon_id;
                    }
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
